<?php

App::uses('Crud', 'Model');

class Theme extends Crud {

    /** Al Agregar/eliminar/actualizar las constantes de los modelos implica ir
     * y actualizar los comentarios en los campos de la tabla asociada a este modelo.
     */
    const STATUS_ENABLED = 1;
    const STATUS_DISABLED = 0;
    const TYPE_ASSISTANCE = 1;
    const TYPE_CALIFICATION = 2;

    /**
     * Obtenemos los tipos de temas
     * @return array
     */
    public static function getTypes() {
        return array(
            array(
                'text' => __("Asistencia"),
                'value' => self::TYPE_ASSISTANCE,
            ),
            array(
                'text' => __("Calificación"),
                'value' => self::TYPE_CALIFICATION,
            ),
        );
    }

    /**
     * Obtenemos los registros para el grid del admin
     * @return type
     */
    public function getEnabled() {
        $order = array('name' => 'ASC');
        $records = $this->findAllByStatus(self::STATUS_ENABLED, array(), $order);
        return $this->flatArray($records);
    }

    /**
     * Obtenemos las lineas por area
     * @param type $testId
     * @return type
     */
    public function getEnabledByTest($testId) {
        $filters = array(
            'fields' => array(
                'id',
                'course',
                'theme',
                'duration',
                'attendant',
                'type',
            ),
            'conditions' => array(
                'status' => self::STATUS_ENABLED,
                'test_id' => $testId,
            ),
            'order' => array(
                'orden' => 'ASC',
                'course' => 'ASC',
                'theme' => 'ASC',
            ),
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }
    
    public function getByTestAndOrden($testId, $orden) {
        $filters = array(
            'fields' => array(
                'id',
                'course',
                'theme',
                'duration',
                'attendant',
                'type',
            ),
            'conditions' => array(
                'orden' => $orden,
                'test_id' => $testId,
            ),
            'order' => array(
                'course' => 'ASC',
                'theme' => 'ASC',
            ),
        );
        return $this->find('first', $filters);
    }

    public function getThemesByTestId($testId) {
        $filters = array(
            'fields' => array(
                'theme',
            ),
            'conditions' => array(
                'status' => self::STATUS_ENABLED,
                'test_id' => $testId,
            ),
            'order' => array(
                'orden' => 'ASC',
                'course' => 'ASC',
                'theme' => 'ASC',
            ),
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }
}
